﻿using System;
using System.IO;

namespace ECommon
{
    /// <summary>
    /// 路径工具
    /// </summary>
    public static partial class PathTool
    {
        /// <summary>
        /// 仅获取全路径的目录部分
        /// </summary>
        /// <param name="fullPath">全路径</param>
        /// <returns>目录</returns>
        public static string GetOnlyDirectory(string fullPath)
        {
            return Path.GetDirectoryName(fullPath);
        }

        /// <summary>
        /// 根据路径创建对应目录
        /// </summary>
        /// <param name="path">路径</param>
        public static void CreateDirectory(string path)
        {
            string fullPath = Path.GetFullPath(path);
            string directory = GetOnlyDirectory(fullPath);
            if (directory == null || string.IsNullOrEmpty(directory.Trim()))
            {
                return;
            }
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
        }

        /// <summary>
        /// 格式化文件路径
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>格式化后的文件路径</returns>
        /// <exception cref="ArgumentNullException">参数为空异常</exception>
        public static string FormatFilePath(string filePath)
        {
            if (filePath == null || string.IsNullOrEmpty(filePath.Trim()))
            {
                throw new ArgumentNullException("filePath", "文件路径为空");
            }
            filePath = filePath.Replace('\\', Path.DirectorySeparatorChar);
            return filePath;
        }
    }
}